<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Member;
use App\Models\UserTitle;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class MemberController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Member(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('cover')->image('',100,80);
            $grid->column('name');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Member(), function (Show $show) {
            $show->field('id');
            $show->field('name');
            $show->field('origin_price');
            $show->field('price');
            $show->field('valid_time');
            $show->field('intro');
            $show->field('cover');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Member(['items']), function (Form $form) {
            $form->display('id');
            $form->text('name')
                 ->maxLength(60)
                 ->required();
            $form->image('cover')
                ->disk('oss')
                ->removable(false)
                ->autoUpload()
                ->uniqueName()
                ->saveFullUrl()
                ->required();

            $form->image('badge','徽章')
                ->disk('oss')
                ->removable(false)
                ->autoUpload()
                ->uniqueName()
                ->saveFullUrl()
                ->required();


            $form->hasMany('items','价格',function (Form\NestedForm $form){
                $form->number('valid_time')
                    ->min(1)
                    ->max(24)
                    ->required();
                $form->currency('origin_price')
                    ->symbol('￥')
                    ->required();
                $form->currency('price')
                    ->symbol('￥')
                    ->required();
            });


            $form->number('integral_doubling','积分翻倍')
                 ->required();

            $form->number('post_recruitment','发布猎聘数')
                ->required();

            $form->select('user_title_id','可查看内容')
                  ->options(UserTitle::getOption())
                  ->saving(function ($value){
                     return (int)$value;
                  })->help('不选择将可查看全平台内容');


            $form->number('resume_download','简历下载数')
                ->required();

            $form->number('order_ad','接单广告数')
                ->required();


            $form->number('consult_num','咨询次数')
                ->required();

            $form->number('publish_video','发布视频秒数')
                ->required();


            $form->number('create_circle','创建圈子数')
                ->required();

            $form->text('exclusive_customer_service','专属客服电话')
                 ->required();


            $form->editor('intro')
                 ->required();


            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
